package com.emuporium.gymrat.server.api;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.simpleframework.xml.Serializer;
import org.simpleframework.xml.core.Persister;

import com.emuporium.gymrat.server.data.Exercise;
import com.emuporium.gymrat.server.data.ExerciseDB;

@SuppressWarnings("serial")
public class ApiExerciseServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/xml");
		PrintWriter out = response.getWriter();
		int exerciseId, userId;
		do {
			try {
				exerciseId = new Integer(request.getParameter("id"));
			} catch (NumberFormatException e) {
				out.println("Bad exerciseId format");
				break;
			}
			try {
			userId = new Integer(request.getParameter("user"));
			} catch (NumberFormatException e) {
				out.println("Bad userId format");
				break;
			}

			Exercise exercise = ExerciseDB.selectExercise(exerciseId, userId);

			Serializer serializer = new Persister();

			try {
				serializer.write(exercise, out);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				out.println("oops");
			}
		} while (false);
		out.close();
	}

}
